package cn.etynit.org.controller;
import cn.etynit.basic.anno.LogAnno;
import cn.etynit.basic.util.AjaxResult;
import cn.etynit.basic.util.PageList;
import cn.etynit.org.domain.SDdetail;
import cn.etynit.org.domain.SDtype;
import cn.etynit.org.query.SDtypeQuery;
import cn.etynit.org.service.SDtypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sdt")
@Api(value = "数据字典的API", description = "数据字典相关的CRUD功能")
@LogAnno
public class SDtypeController {

    @Autowired
    private SDtypeService sDtypeService;

    // 查询所有
    @GetMapping
    @ApiOperation(value = "查询所有员工数据")
    public List<SDtype> findAll() {
        List<SDtype> list = sDtypeService.findAll();
        System.out.println(list);
        return list;
    }


    // 分页查询
    @PostMapping
    @LogAnno(method = "queryByPage", methodDes = "查询数据字典类型")
    public PageList<SDtype> queryByPage(@RequestBody SDtypeQuery sDtypeQuery){
        return sDtypeService.queryByPage(sDtypeQuery);
    }

    // 根据id删除
    @DeleteMapping("/{id}")
    @LogAnno(method = "delete", methodDes = "删除数据字典类型")
    public AjaxResult delete(@PathVariable("id")Long id){
        sDtypeService.delete(id);
        return AjaxResult.me().success();
    }

    // 批量删除
    @PatchMapping
    @LogAnno(method = "batchDel", methodDes = "批量删除数据字典类型")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        sDtypeService.batchDel(ids);
        return AjaxResult.me().success();
    }

    // 新增和修改
    @PutMapping
    @LogAnno(method = "addOrupdate", methodDes = "添加或者修改数据字典类型")
    public AjaxResult add(@RequestBody SDtype sDtype){
        if (null == sDtype.getId()){
            //新增
            sDtypeService.add(sDtype);
        } else {
            // 修改
            sDtypeService.update(sDtype);
        }
        return AjaxResult.me().success();
    }

    // 抽屉
    @GetMapping("/findById/{id}")
    public List<SDdetail> findById(@PathVariable("id") Long id){
        return sDtypeService.findById(id);
    }
}
